<script setup lang="ts">
import { state, formState } from './state';
import { getCurrentInstance, redirectTo, showToast } from '@tarojs/taro';
import { FORWARD_APP_TASK } from '/@/api/modules/maintain/plan-task';
import { Right as AppNutIconRight } from '@nutui/icons-vue-taro';
/**
 * @description 当前路由信息
 */
const params = getCurrentInstance().router?.params as {
  id: string;
};

function submit() {
  if (!formState.planTaskEventDto.assignUserName) {
    showToast({
      title: '请选择处理人',
      icon: 'error',
    });
    return;
  }
  formState.planTaskEventDto.planTaskId = params.id;

  FORWARD_APP_TASK(formState).then((res) => {
    if (res.comResp?.code === 100) {
      showToast({
        title: '转派成功',
        icon: 'success',
        success: () => {
          setTimeout(() => {
            formState.planTaskEventDto = {
              planTaskId: '',
              assignUserId: '',
              assignUserName: '',
              opinion: '',
            };
            redirectTo({
              url: `/pages/inspection/forward-success/index?id=${params.id}`,
            });
          }, 2000);
        },
      });
    }
  });
}
</script>
<template>
  <view class="inspection-forward app-page--1">
    <app-navbar title="转派" />

    <scroll-view class="app-page__wrapper" :scroll-y="true" :lower-threshold="100">
      <view class="inspection-forward__wrapper">
        <nut-form :model-value="formState">
          <nut-form-item label="选择处理人">
            <view
              class="inspection-forward__assign-user-wrapper"
              @click="state.appUserListPopupIsShow = !state.appUserListPopupIsShow"
            >
              <text>{{ formState.planTaskEventDto.assignUserName || '请选择' }}</text>
              <app-nut-icon-right />
            </view>
          </nut-form-item>
          <nut-form-item class="nut-form__title-wrapper" label="转派意见" />
          <nut-form-item>
            <nut-textarea
              v-model="formState.planTaskEventDto.opinion"
              placeholder="填写转派意见"
              rows="8"
              limit-show
              max-length="200"
              text-align="left"
            />
          </nut-form-item>
        </nut-form>
      </view>
    </scroll-view>
    <view class="inspection-forward__footer">
      <nut-button block type="info" @click="submit"> <text>提交</text></nut-button>
    </view>
    <app-user-list-popup
      v-model="state.appUserListPopupIsShow"
      title="处理人"
      @on-selected="(user: AppSysUser.Dto) => {
        formState.planTaskEventDto.assignUserName = user.name;
        formState.planTaskEventDto.assignUserId = user.uaaUserId;
      }"
    />
  </view>
</template>
<style lang="scss">
@import './index.scss';
</style>
